package cn.cxq.learning.e2_3;

public class CosUtil {

    private static double PI = 3.1415926535897932;

    private static int getSinItemNumber(double x) {
        int N;
        if (x < 0) x = -x;
        if (x <= PI / 8) N = 6;
        else if (x <= PI / 4) N = 8;
        else if (x <= PI / 2) N = 10;
        else if (x <= 3 * PI / 4) N = 12;
        else N = 14;
        return N;
    }

    public static double cosTNV(double x) {
        int K, N;
        double y = 1.0, xx;
        N = getSinItemNumber(x) + 1;
        K = N * 2;
        xx = x * x;
        while (K > 0) {
            y = 1.0 - y * xx / K / (K - 1);
            K -= 2;
        }
        return y;
    }
}
